import sys
import numpy as np

def verify_result(output_path, golden_path):
    output = np.fromfile(output_path, dtype=np.int8).reshape(-1)
    golden = np.fromfile(golden_path, dtype=np.int8).reshape(-1)
    
    # 检查长度是否一致
    if len(output) != len(golden):
        print(f"长度不匹配 - 输出: {len(output)}, 黄金: {len(golden)}")
        return False
    
    # 查找差异索引
    diff_idx = np.where(output != golden)[0]
    
    # 输出前50个差异
    for i in range(min(50, len(diff_idx))):
        idx = diff_idx[i]
        print(
            "data index: %06d, expected: %d, actual: %d" %
            (idx, golden[idx], output[idx])
        )
    
    # 计算错误率
    error_ratio = float(len(diff_idx)) / len(golden)
    print(f"error ratio: %.4f" % error_ratio)
    
    # 错误率为0则通过
    return len(diff_idx) == 0


if __name__ == '__main__':
    try:
        if len(sys.argv) != 3:
            print("Usage: python verify_result.py <output_out.bin> <golden.bin>")
            sys.exit(1)
            
        res = verify_result(sys.argv[1], sys.argv[2])
        if not res:
            raise ValueError("[ERROR] result error")
        else:
            print("test pass")
    except Exception as e:
        print(e)
        sys.exit(1)
